package kpi.java_training.domain.teaching;

import kpi.java_training.domain.questions.Question;
import kpi.java_training.domain.questions.QuestionData;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by Alex Kebkalo on 12.11.13.
 */
public class SameComplexityQuestions {
    private int complexity;
    private List<Question> bucket;
    private List<Question> usedBucket;

    public int getComplexity() {
        return complexity;
    }

    public SameComplexityQuestions(int complexity) {
        this.complexity = complexity;
        bucket = new LinkedList<>();
        usedBucket = new LinkedList<>();
    }

    public void add(Question question) {
        if (question.getComplexity() == complexity) {
            bucket.add(question);
        }
    }

    public boolean parseUserAnswer(String userAnswer) {
        if (bucket.isEmpty()) {
            return true;
        }
        boolean result = bucket.get(0).parseUserAnswer(userAnswer);
        if (!result) {
            usedBucket.add(bucket.get(0));
        }
        bucket.remove(0);
        if (bucket.isEmpty()) {
            bucket = usedBucket;
            usedBucket = new LinkedList<>();
        }
        return result;
    }

    public String getQuestionMessage() {
        if (bucket.isEmpty()) {
            return "";
        }
        return bucket.get(0).toString();
    }

    public QuestionData getQuestionData() {
        if (bucket.isEmpty()) {
            return null;
        }
        return bucket.get(0).getSaveData();
    }

    public int getBucketSize() {
        return bucket.size();
    }
}
